<template>
  <div >
    <el-form  label-width="80px" style="text-align: left;">
      <el-form-item label="病人姓名">
        <el-input v-model="param.caseName" :readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="退药医生" >
        <el-select placeholder="请选择" v-model="param.workerId">
        <el-option :label="doctor.wkName" :key="doctor.id" :value="doctor.id" v-for="doctor in allDoctor"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请选择退药的药品及数量" label-width="178px">
        <el-cascader
          :options="transformedOptions"
          v-model="selectedOptions"
          @change="handleChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="退药理由">
        <el-input type="textarea" v-model="param.returnCause"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addMedicineReturnDetail">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="wdName"
        label="药品名称"
        >
      </el-table-column>
      <el-table-column
        prop="wdNum"
        label="退药数量"
        >
      </el-table-column>
      <el-table-column
        prop="wdBuyPrice"
        label="药品售价/盒">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="delDetail(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-button type="primary" @click="addmedicineReturnRequest">确定</el-button>
  </div>
</template>

<script>
export default {
  props:{
        message: {
          type: Object,
          required: true
        }
      },

      
  data() {
    return {
      originalData: [
          // { id: 56, mdBuyPrice: 15, mdDrugStoreId: "DRUS01", mdName: "999感冒灵", mdNum: 3, mdPrescriptionId: "1315865106541707264" },
          // { id: 57, mdBuyPrice: 16, mdDrugStoreId: "DRUS00", mdName: "复方氨酚烷胺片", mdNum: 3, mdPrescriptionId: "13158651065417072653" },
          // { id: 58, mdBuyPrice: 17, mdDrugStoreId: "DRUS02", mdName: "阿莫西林胶囊", mdNum: 3, mdPrescriptionId: "13158651065417072653" }
        ],
      tableData:[],
      param:{},
      allDoctor:[],
      transformedOptions: [], // 将会存储转换后的数据
      selectedOptions: [], // 用于存储用户的选择
      SfgNum:'',
    }
  },

  methods: {
    queryAllDoctor(){
      this.$axios.get('api/doctor/queryAllDoctor').then(resp=>{
        if(resp.data.code==200){
          this.allDoctor=resp.data.data
          // console.log(resp.data)
        }
      })
    },

    queryMedicineReturnDetail(){
      this.$axios.get('api/doctor/queryMedicineReturnDetail/'+this.param.caseId).then(resp=>{
        this.originalData=resp.data.data
        this.transformedOptions = this.transformData(resp.data.data);
        // console.log(resp.data.data)
      })
    },

    transformData(data) {
      return data.map(item => ({
        value: item.mdDrugStoreId, // 一级选项的值
        label: item.mdName, // 一级选项的标签
        children: [{ // 二级选项
          value: item.mdNum,
          label: `库存: ${item.mdNum}`
        }]
      }));
    },

    handleChange(value) {
      this.param.wdDrugStoreId=value[0],
      this.param.wdNum=value[1]
    },

    queryAllMedicineReturnDetail(){
      this.$axios.get('api/doctor/queryAllMedicineReturnDetail/'+this.SfgNum).then(resp=>{
        if(resp.data.code==200){
          this.tableData=resp.data.data
          console.log(resp.data)
        }
      })
    },

    addMedicineReturnDetail(){
      this.param.wdWithdrawalId=this.SfgNum
      this.$axios.post('api/doctor/addMedicineReturnDetail',this.param).then(resp=>{
        if(resp.data.code==200){
          this.queryAllMedicineReturnDetail()
          this.$message({
                showClose: true,
                message: '已添加',
                type: 'success'
            });
        }
      })
    },

    delDetail(id){
      this.$axios.get('api/doctor/delDetail/'+id).then(resp=>{
        if(resp.data.code==200){
          this.queryAllMedicineReturnDetail()
          this.$message({
                showClose: true,
                message: '已删除',
                type: 'success'
            });
        }
      })
    },

    addmedicineReturnRequest(){
      this.$axios.post('api/doctor/addmedicineReturnRequest',this.param).then(resp=>{
        this.$message({
                showClose: true,
                message: '以提交申请',
                type: 'success'
            });
        this.tableData=''
        
      })
    },

    getSfgNum(){
      this.$axios.get('api/doctor/createSfg').then(resp=>{
        this.SfgNum=resp.data.data
      })
    },

    text(){
      this.param.caseId=this.message.caseId
      this.param.caseName=this.message.caseName
     }
  },


  created(){
    this.text()
    this.queryAllDoctor()
    this.queryMedicineReturnDetail()
    this.getSfgNum()
  },

}
</script>

<style scoped>
</style>